<?php
require_once('MySqlConnection.php');
require_once('Address.php');

class AddressDAL{
	private $_connection;
	
	//SINGLETON PATTERN
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new AddressDAL();
		return self::$_instance;
	}
	//SINGLETON PATTERN END
	
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
	
	public function getAddresses(){
		$this->_connection->Open();
		$sqlString = "select * from address";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'address');
	}
	
	public function getAddressById($id){
		$this->_connection->Open();
		$sqlString = "select * from address where id = '{$id}'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'address');
	}
	
	public function addAddress($address){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("INSERT INTO address(street,house_number,box,location_id,city_name,latitude,longitude) VALUES(?,?,?,?,?,?,?)")) {
			$stmt->bind_param("sssisdd", $address['street'], $address['house_number'], $address['box'], $address['location_id'], $address['city_name'], $address['lat'], $address['lng']);
			$success = $stmt->execute();
			if(!$success){
				echo "Inserting address failed!";
			}
			
			else{
				$resultArray[] = $mysqli->insert_id;
				return $resultArray; 
			}
		}
		else{
			
			echo "Failed preparing insert address statement!";
		}
		$mysqli -> close();
	}
	
	public function removeAddress($addressId){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("DELETE FROM address WHERE id = '?'")) {
			$stmt->bind_param("i", $addressId);
			$success = $stmt->execute();
			if(!$success){
				echo "Removing address failed!";
			}
		}
		else{
			echo "Failed preparing delete address statement!";
		}
		$mysqli -> close();
	}
	
	public function updateAddress($address) {
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("UPDATE address SET street = ?, house_number = ?, box = ?, location_id = ?, city_name = ?, latitude = ?, longitude = ? WHERE id = ?")) {
			$stmt->bind_param("sssisddi", $address['street'], $address['house_number'], $address['box'], $address['location_id'], $address['city_name'],$address['lat'], $address['lng'], $address['id']);
			$success = $stmt->execute();
			if(!$success){
				echo "Updating address failed!";
			}
		}
		else{
			echo "Failed preparing update address statement!";
		}
		$mysqli -> close();
	}

	//MAPPING	
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	
	private function Map($data,$resultType){
		$object = new Address($data->id,$data->street,$data->house_number,$data->box,$data->location_id,$data->city_name,$data->latitude,$data->longitude);
		return $object;
	}
	//MAPPING END
}


